import {Injectable} from '@angular/core';
import {AjaxService} from '../../public/service/ajax.service';
import {HttpCodesEnum} from '../../public/setting/enums';
import {SettingUrl} from '../../public/setting/setting_url';
import {NzMessageService, NzNotificationService} from 'ng-zorro-antd';

@Injectable({
  providedIn: 'root'
})
export class SettingService {

  constructor(private ajaxService: AjaxService, private notification: NzNotificationService, private message: NzMessageService) {
  }

  /**
   * 查询设置列表
   * @returns {any<T>}
   */
  list(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.setting.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.info);
        }
      });
    });
  }

  /**
   * 查看
   * @returns {Promise<any>}
   */
  loadByK(key) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.setting.loadByK + key,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.info);
        }
      });
    });
  }

  /**
   * 修改
   * @returns {Promise<any>}
   */
  modify(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.setting.modify,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
            me.message.success(res.info);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.info);
        }
      });
    });
  }
}
